package OliveWindow;

# Copyright (c) 2005, Shawn Boyette
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#  * Neither the name of the Olive Newsreader  nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

require Exporter;
use warnings;
use strict;

our @ISA       = qw(Exporter);
our @EXPORT    = qw( &wininit &winflip &winstatus );

#######################################################################
##  window and system logic  ##########################################
#######################################################################

sub winflip {
    my $cui = shift;
    my $focus = 0;

    if ($_[0] && ($_[0] eq 'story' or $_[0] eq 'news')) {
        $focus = $_[0];
    } else {
        $focus = $cui->userdata->{focus};
    }

    my $w   = $cui->userdata->{wins};
    my $msg = '';

    if ($focus eq 'news') {
        $w->{news}{ftr2}->text('V');
        $w->{news}{ftr2}->draw;
        $msg = $cui->userdata->{chan} if $cui->userdata->{chan};
        $w->{news}{ftr1}->text($msg . ' ' x ($w->{dim}[0] - length($msg)));
        $w->{news}{ftr1}->draw;
        $w->{story}{view}->focus;
        $cui->userdata->{focus} = 'story';
    } else {
        $w->{news}{ftr2}->text('^');
        $w->{news}{ftr2}->draw;
        $w->{news}{ftr1}->text($msg . ' ' x ($w->{dim}[0] - length($msg)));
        $w->{news}{ftr1}->draw;
        $w->{news}{list}->focus;
        $cui->userdata->{focus} = 'news';
    }
}

sub wininit {
    my ($cui,$root,$rev) = @_;
    my %w    = ();

    $rev =~ s/\D//g;
    $rev = uc(sprintf("%4.4x",$rev));

    my $height = $root->height;
    my $width  = $root->width - 1;
    my $r = $height % 2;
    $w{dim}[0] = $width;
    $w{dim}[1] = $height;
    $w{dim}[2] = $r;

    $w{news} = $root->add('news', 'Window',
                          -border => 0,
                          -height => int($height / 2),
                         );
    $w{news}{head} = $w{news}->add('header', "Label",
                                   -bg   => 'blue',
                                   -fg   => 'white',
                                   -text => "Olive r$rev" . ' ' x ($width - 10),
                                   -bold => 1,
                                  );
    $w{news}{ftr1} = $w{news}->add('footer1', "Label",
                                   -bg    => 'blue',
                                   -text  => ' ' x $width,
                                   -width => $width,
                                   -y     => int($height / 2) - 1,
                                  );
    $w{news}{ftr2} = $w{news}->add('footer2', "Label",
                                   -bg    => 'blue',
                                   -bold  => 1,
                                   -text  => '^',
                                   -width => 1,
                                   -x     => $width,
                                   -y     => int($height / 2) - 1,
                                  );

    $w{story} = $root->add( 'story', 'Window',
                            -border => 0,
                            -height => int($height / 2) + $r,
                            -y => int($height / 2),
                          );
    $w{story}{view} = $w{story}->add('storyview', 'TextViewer',
                                     -wrapping   => 1,
                                     -text       => "",
                                     -vscrollbar => 1,
        );
    return \%w;
}

sub winstatus {
    my $cui = shift;
    my $w   = $cui->userdata->{wins};
    my $msg = '';

    my $cur = $w->{news}{list}->get_active_id + 1;
    my $max = $w->{news}{list}->{-max_selected} + 1;

    $msg = "Story $cur of $max (".$cui->userdata->{ur}." unread)";

    $w->{news}{ftr1}->text($msg . ' ' x ($w->{dim}[0] - length($msg)));
    $w->{news}{ftr1}->draw;
}


1;
